/*********************************************************************************
 *      Copyright:  (C) 2022 Li Yanting<liyanting2@163.com>
 *                  All rights reserved.
 *
 *       Filename:  serial.h
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(05/06/2022)
 *         Author:  Li Yanting <liyanting2@163.com>
 *      ChangeLog:  1, Release initial version on "05/06/2022 08:15:40"
 *                 
 ********************************************************************************/

#ifndef _SERIAL_H_
#define _SERIAL_H_

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include "logger.h"

#define SERIAL_NAME_LEN 64

typedef struct attr_ctx_s
{
    int                fd;            //串口文件描述符
    int                baudrate;      //波特率
    int                databits;      //数据位
    char               parity;        //奇偶校验位
    int                stopbits;      //停止位
    int                max_send_len;  //单次最大发送长度
    char               serial_name[SERIAL_NAME_LEN]; //串口名称
    struct termios     original_termios; //串口的原始属性
} attr_ctx_t;


/*********************************************************************************
 *      Function name:  serial_open
 *        Description:  open serial 
 *         @parameter:  attr
 *       Return value:  On error, -1 is returned. On success, 0 is returned.
 ********************************************************************************/
int serial_open(attr_ctx_t *attr);


/*********************************************************************************
 *      Function name:  serial_close
 *        Description:  close serial 
 *         @parameter:  attr
 *       Return value:  On error, -1 is returned. On success, 0 is returned.
 ********************************************************************************/
int serial_close(attr_ctx_t *attr);


/*********************************************************************************
 *      Function name:  serial_init
 *        Description:  serial init
 *         @parameter:  attr
 *       Return value:  On error, -1 is returned. On success, 0 is returned.
 ********************************************************************************/
int serial_init(attr_ctx_t *attr);


/*********************************************************************************
 *      Function name:  serial_send
 *        Description:  send AT or message by serial
 *         @parameter:  attr  the struct of serial attr
 *         @parameter:  sbuf  a buf for recv message
 *         @parameter:  sbuf_len  buf size (strlen)
 *       Return value:  On error, -1 is returned. On success, 0 is returned.
 ********************************************************************************/
int serial_send(attr_ctx_t *attr, char *sbuf, int sbuf_len);


/*********************************************************************************
 *      Function name:  serial_recv
 *        Description:  receive message from serial
 *         @parameter:  attr  the struct of serial attr
 *         @parameter:  rbuf  a buf for recv message
 *         @parameter:  rbuf_size  buf size (sizeof)
 *         @parameter:  timeout  listen recv time for select
 *       Return value:  On error, -1 is returned. On success, 0 is returned.
 ********************************************************************************/
int serial_recv(attr_ctx_t *attr, char *rbuf, int rbuf_size, int timeout);

#endif
